package top.itaso.app.ui.treelevel.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import top.itaso.app.R;
import top.itaso.lib.widget.helper.NodeTreeBaseAdapter;
import top.itaso.app.listener.INodeTreeCheckedChangeListener;
import top.itaso.lib.widget.vo.Node;

public class NodeTreeAdapter extends NodeTreeBaseAdapter {

    private INodeTreeCheckedChangeListener checkedChangeListener;

    public void setCheckedChangeListener(INodeTreeCheckedChangeListener checkedChangeListener) {
        this.checkedChangeListener = checkedChangeListener;
    }

    public NodeTreeAdapter(List<Node> datas, int defaultExpandLevel, int iconExpand, int iconUnExpand) {
        super(datas, defaultExpandLevel, iconExpand, iconUnExpand);
    }

    public NodeTreeAdapter(List<Node> datas, int defaultExpandLevel) {
        super(datas, defaultExpandLevel);
    }

    @Override
    public void onBindViewHolder(final Node node, final RecyclerView.ViewHolder holder, final int position) {

        final NodeViewHolder viewHolder = (NodeViewHolder) holder;

        viewHolder.tvName.setText(node.getTitle());

        if (node.getIcon() == -1) {
            viewHolder.ivExpand.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.ivExpand.setVisibility(View.VISIBLE);
            viewHolder.ivExpand.setImageResource(node.getIcon());
        }

        viewHolder.driver.setVisibility((node.getParent() == null && position > 0) ? View.VISIBLE : View.GONE);

        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setChecked(node, viewHolder.checkBox.isChecked());

                if (checkedChangeListener != null) {
                    checkedChangeListener.onCheckChange(node, position, viewHolder.checkBox.isChecked());
                }
            }
        });

        if (node.isChecked()) {
            viewHolder.checkBox.setChecked(true);
        } else {
            viewHolder.checkBox.setChecked(false);
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View nodeView = View.inflate(parent.getContext(), R.layout.layout_multilevel_list_item, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        nodeView.setLayoutParams(layoutParams);
        return new NodeViewHolder(nodeView);
    }

    static class NodeViewHolder extends RecyclerView.ViewHolder {
        private CheckBox checkBox;
        private TextView tvName;
        private ImageView ivExpand;
        private View driver;


        public NodeViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.node_checkbox);
            tvName = itemView.findViewById(R.id.note_name);
            ivExpand = itemView.findViewById(R.id.node_expand);
            driver = itemView.findViewById(R.id.driver);
        }
    }
}
